//
//  SEGTrackMaterelView.m
//  Uhomebk
//
//  Created by mac on 2017/10/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGTrackMaterelView.h"
#import "SEGTrackMatrelsController.h"
#define kVlineViewX 69.5
#define kVlineViewWidth 0.5
@implementation SEGTrackMaterelView

-(instancetype)initWithFrame:(CGRect)frame trackModel:(SEGOrderTrackInfoModel *)trackModel
{
    if (self = [super initWithFrame:frame]) {
        [self setupUIWithFrame:frame trackModel:trackModel];
    }
    return self;
}

/**
 UI布局
 */
-(void)setupUIWithFrame:(CGRect)frame trackModel:(SEGOrderTrackInfoModel *)trackModel
{
    UIView *trackOtherView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    trackOtherView.backgroundColor = kColor_W;
    [self addSubview:trackOtherView];
    CGFloat otherViewHeight = 0;
    //2.1子任务节点标识
    /* 节点名字*/
    UILabel *otherNameIcon = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, 12, 33, 15)];
    otherNameIcon.font = kFont_11;
    [otherNameIcon setTextColor:kColor_1];
    otherNameIcon.backgroundColor = kColor_bg;
    otherNameIcon.layer.cornerRadius = 8;
    otherNameIcon.layer.masksToBounds = YES;
    otherNameIcon.textAlignment = NSTextAlignmentCenter;
    otherNameIcon.text = @"物料";
    /* 竖线*/
    UIView *vLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX, 0, 0.5, 0)];
    vLineView.backgroundColor = kColor_L2;
    /* 节点*/
    
    UIImageView *otherIcon = [[UIImageView alloc] initWithFrame: CGRectMake(kVlineViewX-4.5, CGRectGetMidY(otherNameIcon.frame)-5, 10, 10)];
    otherIcon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_orderpoint_gray_small")];
    
    /* 物料信息内容*/
    //计算文案宽度
    NSString *materelsStr = [NSString stringWithFormat:@"%@ 申请了物料，",[self getCurrUserName:trackModel.currentUser]];
    CGSize materelsLabelSize = STRING_SIZE_FONT(1000, materelsStr, 15);
    UILabel *materelsLabel = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(otherNameIcon.frame)+12, materelsLabelSize.width, 15)];
    [materelsLabel setTextColor:kColor_1];
    materelsLabel.font = kFont_15;
    materelsLabel.text = materelsStr;
        /* 查看物料清单按钮*/
    CustomButtonView *materelsButton = [[CustomButtonView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(materelsLabel.frame), CGRectGetMaxY(otherNameIcon.frame)+12, 80, 15)];
    [materelsButton setTitle:@"物料清单" forState:UIControlStateNormal];
    [materelsButton setTitleColor:kColor_S3 forState:UIControlStateNormal];
    materelsButton.titleLabel.font = kFont_15;
    materelsButton.tempData = trackModel.materiels;
    [materelsButton addTarget:self action:@selector(checkMaterelsAction:) forControlEvents:UIControlEventTouchUpInside];
 
    CGFloat priceY = CGRectGetMaxY(materelsLabel.frame)+12;
    /* 总价*/
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, priceY, self.frame.size.width-kVlineViewX-30, 15)];
    [priceLabel setTextColor:kColor_1];
    priceLabel.font = kFont_15;
    priceLabel.text = [NSString stringWithFormat:@"总计：%.2f元",[trackModel.charge floatValue]/100];
    
    //重新适配高度
    otherViewHeight = CGRectGetMaxY(priceLabel.frame);
    vLineView.height = otherViewHeight;
    self.height = otherViewHeight;
    trackOtherView.height = otherViewHeight;
    
    [trackOtherView addSubview:vLineView];
    [trackOtherView addSubview:otherNameIcon];
    [trackOtherView addSubview:priceLabel];
    [trackOtherView addSubview:otherIcon];
    [trackOtherView addSubview:materelsButton];
    [trackOtherView addSubview:materelsLabel];
}

-(NSString *)getCurrUserName:(NSArray *)userArray
{
    if (userArray.count == 0) {
        return @"";
    }
    __block NSString *currUser = @"";
    [userArray enumerateObjectsUsingBlock:^(SEGOrderCurrentUser * _Nonnull userModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if (userArray.count > 1  ) {
            if (userModel.userName.length>0) {
                if (idx == 0) {
                    currUser = userModel.userName;
                }
                else{
                    currUser = [currUser stringByAppendingString:[NSString stringWithFormat:@",%@",userModel.userName]];
                }
                
            }
        }
        else{
            currUser = userModel.userName;
        }
    }];
    return currUser;
}

-(void)checkMaterelsAction:(CustomButtonView *)sender
{
    SEGTrackMatrelsController *VC = [[SEGTrackMatrelsController alloc] init];
    VC.dataArray = sender.tempData;
    [self.parentVC.navigationController pushViewController:VC animated:YES];
}

@end
